package cn.bdqn.auth_consumer.controller;

import cn.bdqn.auth_consumer.service.UserService;
import cn.bdqn.common.Constants;
import cn.bdqn.common.Dto;
import cn.bdqn.common.DtoUtil;
import cn.bdqn.vo.UsersVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 2018/9/23
 */
@Api(value="/api", tags="用户注册模块")
@RestController
@RequestMapping("/api")
public class SmsController {


    @Resource
    private UserService userService;
    @Resource
    private JmsMessagingTemplate jmsMessagingTemplate;


    /**
     * 发送验证短信
     * @param usersVo
     * @param phone
     * @param code
     * @return
     */
    @ApiOperation(value="手机短信验证API",httpMethod = "PUT", protocols = "HTTP", produces = "application/json", response = Dto.class,notes="使用手机短信验证")
    @RequestMapping(value = "/validate",method = RequestMethod.PUT,produces = "application/json")
    @ResponseBody
    public Dto smsValidate(@RequestBody UsersVo usersVo,@ApiParam(name="phone",value="手机号",required=true)@RequestParam String phone, @ApiParam(name="code",value="短信验证码",required=true)@RequestParam String code){
        if(!phone.matches(Constants.PHONE_RULE)){
            DtoUtil.returnFail("用户名不符合规则！","10086");
        }
        try {
            if(userService.phoneActivate(phone,code,usersVo)){
                return DtoUtil.returnSuccess("激活成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("激活失败","10086");
        }
        return DtoUtil.returnSuccess("激活失败");

    }


}
